package  net.siufung.market.base.user.provider;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.market.base.permit.spi.IRoleUserCountSpi;
import net.siufung.market.base.permit.spi.dto.RoleUserCountDto;
import net.siufung.market.base.user.mapper.IUserRoleMapper;
import net.siufung.market.base.user.mapper.entity.UserRole;

import java.util.List;

/**
 * @author 陈建峰
 * @date 2023/2/11 10:30 PM
 * @since V1.0.0
 */
@Slf4j
@AllArgsConstructor
public class RoleUserCountProvider implements IRoleUserCountSpi {

    private final IUserRoleMapper userRoleMapper;

    @Override
    public List<RoleUserCountDto> getRoleUserCountList(Integer[] roleIds) {
        List<RoleUserCountDto> roleUserCountDtoList = Lists.newArrayList();
        RoleUserCountDto roleUserCountDto;
        List<UserRole> userRoleList = userRoleMapper.selectList(
                new LambdaQueryWrapper<UserRole>().in(UserRole::getRoleId, Lists.newArrayList(roleIds)));
        int userCount;
        for (Integer roleId: roleIds) {
            userCount = 0;
            roleUserCountDto = new RoleUserCountDto();
            roleUserCountDto.setId(roleId);
            for (UserRole userRole: userRoleList) {
                if(roleId.equals(userRole.getRoleId())){
                    userCount ++;
                }
            }
            roleUserCountDto.setUserCount(userCount);
            roleUserCountDtoList.add(roleUserCountDto);
        }
        return roleUserCountDtoList;
    }

    @Override
    public long getUserCountByRoleId(Integer roleId) {
        return userRoleMapper.selectCount(
                new LambdaQueryWrapper<UserRole>().eq(UserRole::getRoleId, roleId));
    }
}
